package com.jk.garden1.controller;

import com.jk.garden1.entity.UserErEntity;
import com.jk.garden1.entity.UserMiniEntity;
import com.jk.garden1.entity.UserTableEntity;
import com.jk.garden1.entity.UserVisitorEntity;
import com.jk.garden1.service.UserMinService;
import com.jk.garden1.util.Md5UUIDSaltUtil;
import com.jk.garden1.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.UUID;

/**
 * 小程序登录和注册
 */
@RestController
@RequestMapping("loginMin")
public class LoginMin {

    @Autowired
   private UserMinService userMinService;


    /**
     * 获取验证码
     * @param
     * @return
     */
    //手机号登录接口
    @PostMapping("loginsPhoneMin")
    public Map loginMin(@RequestParam("phone") String phone, @RequestParam("password") String password){
        UserMiniEntity staff = userMinService.findByUser(phone);
        if (staff == null) {
            return MsgUtil.returnMsg(250,"账号错误",null);
        } else {
            String salt = staff.getSalt();
            String mySalt = salt.substring(0, 5);
            String md5Code = Md5UUIDSaltUtil.createMd5Code(password + mySalt);
            if (!staff.getPassword().equals(md5Code)) {
                return MsgUtil.returnMsg(251, "密码错误", null);
            } else {
                //通过UUID生成token字符串,并将其以string类型的数据保存在redis缓存中，key为token，value为username
                String token = UUID.randomUUID().toString().replaceAll("-", "");
                staff.setToken(token);
                staff.setPassword(password);
                return MsgUtil.returnMsg(200, "登陆成功", staff);
            }
        }
    }

    /**
     * 修改个人信息
     * @param userMiniEntity
     */
    @RequestMapping("updateUser")
    public void updateUser(@RequestBody UserMiniEntity userMiniEntity){
        userMinService.updateUser(userMiniEntity);
    }
    /**
     * 新增员工信息
     */
    @RequestMapping("addUserEr")
    public void addUserEr(@RequestBody UserErEntity userErEntity) {
        userMinService.addUserEr(userErEntity);
    }

    /**
     * 新增访客
     * @param userVisitorEntity
     */
    @RequestMapping("addUserVisitor")
    public void addUserVisitor(@RequestBody UserVisitorEntity userVisitorEntity) {
        userMinService.addUserVisitor(userVisitorEntity);
    }
    /**
     * 员工申请加入公司
     */
    @RequestMapping("addUserCompany")
    public void addUserCompany(@RequestBody UserMiniEntity userMiniEntity){
        userMinService.addUserCompany(userMiniEntity);
    }

    /**
     * 查询已经预约的信息
     */
    @RequestMapping("selectList")
    public Object selectList(UserMiniEntity userMini){
        return userMinService.selectList(userMini);
    }

    /**
     * 查询单条用户信息
     * @param id
     * @return
     */
    @RequestMapping("queryUserOne")
    public UserMiniEntity queryUserOne(@RequestParam("id") String id){
        return userMinService.queryUserOne(id);
    }

    /**
     * 查询单条用户信息
     * @param id
     * @return
     */
    @RequestMapping("queryUserFkOne")
    public UserVisitorEntity queryUserFkOne(@RequestParam("id") String id){
        return userMinService.queryUserFkOne(id);
    }
    //注册账号
    @RequestMapping("loginadd")
    public Map registerUser(@RequestBody UserMiniEntity userMiniEntity){
        return userMinService.registerUser(userMiniEntity);
    }
}


